package org.phona;


import org.phona.object.Contact;
import org.phona.object.ContactsAdapter;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


public class MainActivity extends Activity {

	public static final String TAG = "MainActivity";

	private ListView mContactList;
	ContactsAdapter ca;
	
	TextView searchBox;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Long start = System.currentTimeMillis();
		setContentView(R.layout.main);
		/*
		TabHost tabs = (TabHost) this.findViewById(R.id.my_tabhost);
		tabs.setup();
		TabSpec tspec1 = tabs.newTabSpec("First Tab");		
		tspec1.setIndicator(getResources().getString(R.string.phone));
		tspec1.setContent(R.id.phone);
		tabs.addTab(tspec1);
		TabSpec tspec2 = tabs.newTabSpec("Second Tab");
		tspec2.setIndicator(getResources().getString(R.string.history));
		tspec2.setContent(R.id.history);
		tabs.addTab(tspec2);
		TabSpec tspec3 = tabs.newTabSpec("Third Tab");
		tspec3.setIndicator(getResources().getString(R.string.contacts));
		tspec3.setContent(R.id.contacts);
		tabs.addTab(tspec3);
		*/

		Log.i(TAG, "Activity State: onCreate()");
		
		//super.onCreate(savedInstanceState);
		//setContentView(R.layout.contact_manager);

		
		final EditText tv = (EditText) findViewById(R.id.filter);
		searchBox = tv;
		
		Button b0 = (Button)findViewById(R.id.b0);
		b0.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Log.i(TAG, "puch 0");
				tv.setText(tv.getText().toString()+"0");
				filter();
			}
		});
		
		
		Button b1 = (Button)findViewById(R.id.b1);
		b1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Log.i(TAG, "puch 1");
				tv.setText(tv.getText().toString()+"1");
				filter();
			}
		});
		
		Button b2 = (Button)findViewById(R.id.b2);
		b2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				tv.setText(tv.getText().toString()+"2");
				filter();
			}
		});
		
		Button b3 = (Button)findViewById(R.id.b3);
		b3.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				tv.setText(tv.getText().toString()+"3");
				filter();
			}
		});
		
		Button b4 = (Button)findViewById(R.id.b4);
		b4.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				tv.setText(tv.getText().toString()+"4");
				filter();
			}
		});
		
		Button b5 = (Button)findViewById(R.id.b5);
		b5.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				tv.setText(tv.getText().toString()+"5");
				filter();
			}
		});
		
		Button b6 = (Button)findViewById(R.id.b6);
		b6.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				tv.setText(tv.getText().toString()+"6");
				filter();
			}
		});
		
		Button b7 = (Button)findViewById(R.id.b7);
		b7.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				tv.setText(tv.getText().toString()+"7");
				filter();
			}
		});
		
		Button b8 = (Button)findViewById(R.id.b8);
		b8.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				tv.setText(tv.getText().toString()+"8");
				filter();
			}
		});
		
		Button b9 = (Button)findViewById(R.id.b9);
		b9.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				tv.setText(tv.getText().toString()+"9");
				filter();
			}
		});
		
		Button back = (Button)findViewById(R.id.back);
		back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(tv.getText().toString().length()>0){
					tv.setText(tv.getText().toString().substring(0, tv.getText().toString().length()-1));
					filter();
				}
			}
		});
		
		ImageView clear = (ImageView)findViewById(R.id.clear);
		clear.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				tv.setText("");
				filter();
			}
		});
		
		
		// Obtain handles to UI objects
		mContactList = (ListView) findViewById(R.id.list_main);
		// Populate the contact list
		ca = new ContactsAdapter(this);
		ca.addOnUserEventListener(new OnUserEventListener() {
			
			@Override
			public void onEvent(Object obj) {
				handle((Contact)obj);
				
			}
		});
		ca.init();
		/*Toast t = Toast.makeText(getApplicationContext(), "loading:"+"treeinserting:",
		          Toast.LENGTH_SHORT);
		          t.setDuration(5000);
		          t.show();
		 */
		mContactList.setAdapter(ca);
	
		
		
			
		Log.i(TAG,"MainActivity Loaded in :"+(System.currentTimeMillis()-start));

	}
	public void handle(Contact contact){
		Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+contact.getPrimaryPhone()));
		startActivity(intent);
	}
	
	void filter(){
		ca.filter(searchBox.getText().toString());
		mContactList.setAdapter(ca);
	}






}

